﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using CNKI.TPI.Web.Admin.IBLL;

namespace CNKI.TPI.Web.Search.DAO
{
    public class UserDAO : BaseDAO, IUserDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public SYS_CMS_USER GetUser(string userName)
        {
            SYS_CMS_USER result = null;
            try
            {
                //Expression<Func<SYS_CMS_USER, bool>> query = c => c.Username == userName;
                ExpressionCombine<SYS_CMS_USER> exp = new ExpressionCombine<SYS_CMS_USER>();
                exp.And("Username", OperatorType.Equal, userName);
                IList<SYS_CMS_USER> userList = Context.Get<SYS_CMS_USER>(exp.Compile());
                if (userList != null && userList.Count > 0)
                {
                    result = userList[0];
                    result.Username = StringHelper.RemoveHighlightFlag(result.Username);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUser()
        {
            return Context.ExecuteSql(SqlBuilder.Build("*", "SYS_CMS_USER"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_IP> GetUserIP(int userID)
        {
            IList<SYS_CMS_USER_IP> result = null;
            try
            {
                //Expression<Func<SYS_CMS_USER_IP, bool>> query = c => c.UrID == userID && c.Flag == 0;
                ExpressionCombine<SYS_CMS_USER_IP> exp = new ExpressionCombine<SYS_CMS_USER_IP>();
                exp.And("UrID", OperatorType.Equal, userID);
                exp.And("Flag", OperatorType.Equal, 0);
                result = Context.Get<SYS_CMS_USER_IP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_IP> GetGroupIP(IList<int> groupID)
        {
            IList<SYS_CMS_USER_IP> result = null;
            try
            {
                if (groupID.Count() == 0)
                {
                    result = new List<SYS_CMS_USER_IP>();
                }
                else
                {
                    ExpressionCombine<SYS_CMS_USER_IP> exp = new ExpressionCombine<SYS_CMS_USER_IP>();
                    foreach (int item in groupID)
                    {
                        exp.Or("UrID", OperatorType.Equal, item);
                    }
                    exp.And("Flag", OperatorType.Equal, 1);
                    result = Context.Get<SYS_CMS_USER_IP>(exp.Compile());
                }

            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_GROUP> GetUserGroup(string userID)
        {
            IList<SYS_CMS_USER_GROUP> result = null;
            try
            {
                //Expression<Func<SYS_CMS_USER_GROUP, bool>> query = c => c.UserID == userID;
                ExpressionCombine<SYS_CMS_USER_GROUP> exp = new ExpressionCombine<SYS_CMS_USER_GROUP>();
                exp.And("UserID", OperatorType.Equal, userID);
                result = Context.Get<SYS_CMS_USER_GROUP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public IList<SYS_CMS_GROUP> GetGroup(IEnumerable<int> groupID)
        {
            IList<SYS_CMS_GROUP> result = null;
            try
            {
                if (groupID.Count() == 0)
                {
                    result = new List<SYS_CMS_GROUP>();
                }
                else
                {
                    ExpressionCombine<SYS_CMS_GROUP> exp = new ExpressionCombine<SYS_CMS_GROUP>();
                    foreach (int item in groupID)
                    {
                        exp.Or("SysID", OperatorType.Equal, item);
                    }
                    result = Context.Get<SYS_CMS_GROUP>(exp.Compile());
                }

            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_ACL_DB_RIGHT> GetDBRight(string id, RightType rightType)
        {
            IList<SYS_CMS_USER_ACL_DB_RIGHT> result = null;
            try
            {
                //Expression<Func<SYS_CMS_USER_ACL_DB_RIGHT, bool>> query = c => c.UrID == id && c.Flag == (int)rightType;
                ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT> exp = new ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT>();
                exp.And("UrID", OperatorType.Equal, id);
                exp.And("Flag", OperatorType.Equal, (int)rightType);
                result = Context.Get<SYS_CMS_USER_ACL_DB_RIGHT>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public SYS_CMS_USER_ACL_DB_RIGHT GetDBRight(int id, int dbid, RightType rightType)
        {
            SYS_CMS_USER_ACL_DB_RIGHT result = null;
            try
            {
                //Expression<Func<SYS_CMS_USER_ACL_DB_RIGHT, bool>> query = c => c.UrID == id && c.Flag == (int)rightType && c.DBID == dbid;
                ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT> exp = new ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT>();
                exp.And("UrID", OperatorType.Equal, id);
                exp.And("Flag", OperatorType.Equal, (int)rightType);
                exp.And("DBID", OperatorType.Equal, dbid);
                IList<SYS_CMS_USER_ACL_DB_RIGHT> content = Context.Get<SYS_CMS_USER_ACL_DB_RIGHT>(exp.Compile());
                if (content != null && content.Count > 0)
                {
                    result = content[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_ACL_DB_RIGHT> GetGroupDBRight(IList<int> groupID)
        {
            IList<SYS_CMS_USER_ACL_DB_RIGHT> result = null;
            try
            {
                if (groupID.Count() == 0)
                {
                    result = new List<SYS_CMS_USER_ACL_DB_RIGHT>();
                }
                else
                {
                    ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT> exp = new ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT>();
                    foreach (int item in groupID)
                    {
                        exp.Or("UrID", OperatorType.Equal, item);
                    }
                    exp.And("Flag", OperatorType.Equal, 1);
                    result = Context.Get<SYS_CMS_USER_ACL_DB_RIGHT>(exp.Compile());
                }

            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获得密级信息
        /// </summary>
        /// <param name="dbID"></param>
        /// <param name="fieldType"></param>
        /// <returns></returns>
        public IList<SYS_CMS_SECURITY> GetSecurityInfo()
        {
            IList<SYS_CMS_SECURITY> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_SECURITY>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 获取登录失败次数
        /// </summary>
        /// <returns></returns>
        public int GetFaidNum()
        {
            int m = 0;
            string ipstr = IPUntil.GetIP();
            string sql = string.Format(@"select LoginStatus from userloginlog 
                                        where createtime  between DATE_SUB(SYSDATE(),INTERVAL 10 MINUTE) and  SYSDATE()
                                        and IP='{0}' ", ipstr);
            if (ConfigHelper.IsAnonymousControlEnabled())
            {
                sql = sql + " and username!='" + ConfigHelper.GetAnonymousAccount() + "'";
            }
            sql += "  order by  createtime desc limit 2";
            DataTable dt = Context.ExecuteSql(sql, "mysql");
            if (null != dt)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (int.Parse(dr[0].ToString()) != (int)LoginUserInfo.MessageFlage.Success)
                    {
                        m++;
                    }
                }
            }
            return m;

        }

    }
}
